package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.LimitAttatchItemErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.entity.LimitAttatchItemDO;
import com.cmc.cloud.cmclink.doc.mapper.LimitAttatchItemMapper;
import com.cmc.cloud.cmclink.doc.service.LimitAttatchItemService;
import com.cmc.cloud.cmclink.doc.vo.limitattatchitemvo.LimitAttatchItemBaseVO;
import com.cmc.cloud.cmclink.doc.vo.limitattatchitemvo.LimitAttatchItemCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.limitattatchitemvo.LimitAttatchItemPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.limitattatchitemvo.LimitAttatchItemUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * 补充材料明细 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class LimitAttatchItemServiceImpl implements LimitAttatchItemService {

    @Resource
    private LimitAttatchItemMapper limitAttatchItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createLimitAttatchItem(LimitAttatchItemCreateReqVO createReqVO) {
        // 插入
        LimitAttatchItemDO limitAttatchItem = BeanUtil.copyProperties(createReqVO, LimitAttatchItemDO.class);
        limitAttatchItemMapper.insert(limitAttatchItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateLimitAttatchItem(LimitAttatchItemUpdateReqVO updateReqVO) {
        // 校验存在
        validateLimitAttatchItemExists(updateReqVO.getId());
        // 更新
        LimitAttatchItemDO updateObj = BeanUtil.copyProperties(updateReqVO, LimitAttatchItemDO.class);
        limitAttatchItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteLimitAttatchItem(Long id) {
        // 校验存在
        validateLimitAttatchItemExists(id);
        // 删除
        limitAttatchItemMapper.deleteById(id);
    }

    private void validateLimitAttatchItemExists(Long id) {
        if (limitAttatchItemMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(LimitAttatchItemErrorCodeConstants.LIMIT_ATTATCH_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public LimitAttatchItemDO getLimitAttatchItem(Long id) {
        return limitAttatchItemMapper.selectById(id);
    }

    @Override
    public List<LimitAttatchItemDO> getLimitAttatchItemByMainId(Long mainId) {
        List<LimitAttatchItemDO> limitAttatchItemDos = limitAttatchItemMapper.selectList(new LambdaQueryWrapperX<LimitAttatchItemDO>().eqIfPresent(LimitAttatchItemDO::getMainId, mainId));
        return limitAttatchItemDos;
    }

    @Override
    public List<LimitAttatchItemDO> getLimitAttatchItemList(Collection<Long> ids) {
        return limitAttatchItemMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LimitAttatchItemDO> getLimitAttatchItemPage(LimitAttatchItemPageReqVO reqVO) {
        LambdaQueryWrapperX<LimitAttatchItemDO> queryWrapperX = new LambdaQueryWrapperX<>();
        return limitAttatchItemMapper.selectPage(reqVO, queryWrapperX);
    }

    @Override
    public List<LimitAttatchItemBaseVO> getLimitAttatchItemByMainIds(List<Long> mainIds) {
        List<LimitAttatchItemBaseVO> limitAttatchItemDos = new ArrayList<>();
        if (CollectionUtil.isEmpty(mainIds)) {
            return limitAttatchItemDos;
        }
        return BeanUtil.copyToList(limitAttatchItemMapper.selectList(new LambdaQueryWrapperX<LimitAttatchItemDO>().in(LimitAttatchItemDO::getMainId, mainIds)), LimitAttatchItemBaseVO.class);
    }
}
